//
//  PPFriendsTimelineViewController.m
//  pickpic
//
//  Created by GaoYongyue on 16/8/28.
//  Copyright © 2016年 MicroDream. All rights reserved.
//

#import "PPFriendsTimelineViewController.h"
#import "PPMessageTimelineViewController.h"
#import "PPFoundViewController.h"

#import "PPUnreadMessageManager.h"

@interface PPFriendsTimelineViewController ()
@end

@implementation PPFriendsTimelineViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"朋友";

    [self updateNavigationBar];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveUnreadMessageSummaryDidChangeNotification) name:PPUnreadMessageSummaryDidChangeNotification object:nil];
}

- (void)didReceiveUnreadMessageSummaryDidChangeNotification {
    [self updateNavigationBar];
}

- (void)updateNavigationBar {
    PPUnreadMessageSummaryObject *summary = [[PPUnreadMessageManager sharedInstance] summaryForType:PPUnreadMessageSummaryTypeMyNotify];

    UIImage *icon = [UIImage imageNamed:@"but-message"];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    [button setImage:icon forState:UIControlStateNormal];
    [button sizeToFit];
    button.imageView.layer.cornerRadius = button.imageView.width/2;
    button.imageView.layer.masksToBounds = YES;

    if(summary != nil && summary.count.integerValue > 0) {
        if([summary.iconURLString length]) {
            [button sd_setImageWithURL:[NSURL URLWithString:summary.iconURLString] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRetryFailed];
        } else {
            [button setImage:[UIImage imageNamed:@"icon-person-70"] forState:UIControlStateNormal];
        }
    }

    UIBarButtonItem *moreItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    UIBarButtonItem *negItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negItem.width = -10;

    self.navigationItem.rightBarButtonItems = @[negItem, moreItem];

    __weak typeof(self)weakSelf = self;
    [button setActionBlock:^(UIControl *control) {
        [MobClick event:@"92"];
        [weakSelf moreItemDidPressed:moreItem];
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)moreItemDidPressed:(UIBarButtonItem *)item {
    PPMessageTimelineViewController *messageViewConroller = [[PPMessageTimelineViewController alloc] init];
    messageViewConroller.summaryType = PPUnreadMessageSummaryTypeMyNotify;
    [self.navigationController pushViewController:messageViewConroller animated:YES];
}

- (NSArray *)didReceiveNewResult:(id)result {
    NSArray *items = [super didReceiveNewResult:result];

    [[PPUnreadMessageManager sharedInstance] markSummariesAsReadForType:PPUnreadMessageSummaryTypeFriend];

    return items;
}

- (UIView *)emptyPromptViewForError:(NSError *)error {
    if(error != nil) {
        UILabel *label = [[UILabel alloc] init];
        label.text = error.networkPrompt;
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor colorWithRGBHex:0x999999];
        [label sizeToFit];

        return label;
    } else {
        DGAlignmentButton *button = [DGAlignmentButton buttonWithType:UIButtonTypeCustom];
        button.style = DGAlignmentButtonStyleImageTop;
        button.spacing = 10;
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.contentEdgeInsets = UIEdgeInsetsMake(20, 20, 20, 20);
        [button setImage:[UIImage imageNamed:@"icon-found-nothing"] forState:UIControlStateNormal];
        [button setTitle:@"去发现感兴趣的人吧" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRGBHex:0x999999] forState:UIControlStateNormal];
        [button sizeToFit];
        [button addTarget:self action:@selector(emptyPromptButtonDidPressed) forControlEvents:UIControlEventTouchUpInside];

        return button;
    }
}

- (NSString *)preferredCategory {
    return @"Friend";
}

- (void)emptyPromptButtonDidPressed {
    [MobClick event:@"150"];
    PPFoundViewController *foundViewController = [[UIStoryboard storyboardWithName:@"Found" bundle:nil] instantiateViewControllerWithIdentifier:@"PPFoundViewController"];
    [self.navigationController pushViewController:foundViewController animated:YES];

}

@end
